import { StorehouseService } from 'src/app/@core/services/storehouse/storehouse.service';
import { Component, Input } from '@angular/core';
import { TableWidthConfig } from 'ng-devui';
import { DeviceService } from 'src/app/@core/services/device.ts/device.service';
import { MaterialService } from 'src/app/@core/services/material/material.service';
@Component({
  selector: 'app-select-position',
  templateUrl: './select-position.component.html',
  styleUrls: ['./select-position.component.scss']
})
export class SelectPositionComponent {
  @Input() data: any;
  isSelected = false;
  displayTableData: any[] = [];
  displayTreeData: any[];
  treeData:[]=[];
  filteredTreeNodes:any
  option:any;//
  tableheight = '230px';
  selectData:any;
  searchName:''
  pager = {
    total: 20,
    pageIndex: 1,
    pageSize: 20
  };
  pageSizeOptions = [10, 20, 30, 50, 100];
  dataTableOptions = {
    columns: [
      {
        field:'radio',
        header:'选择',
        fieldType:'radio',
        fixedLeft:'0px'
      },
      {
        field: 'code',
        header: '仓位编码',
        fieldType: 'text',
        fixedLeft:'50px'
      },
      {
        field: 'name',
        header: '仓位名称',
        fieldType: 'text',
        fixedLeft:'150px'
      }
    ]
  };
  tableWidthConfig: TableWidthConfig[] = [
    {
      field: 'radio',
      width: '50px',
    },
    {
      field: 'code',
      width: '100px'
    },
    {
      field: 'name',
      width: '100px'
    }
  ]
  searchMaterialCategoryValue: any;
  warehouseId: any =null;
  constructor(private StorehouseService:StorehouseService) { }
  ngOnInit(): void {
    if(this.data.warehouseData){
    this.displayTreeData = [this.data.warehouseData]
    this.StorehouseService.getPositions(this.data.warehouseId,null,this.searchName,this.pager.pageIndex,this.pager.pageSize).subscribe((res:any)=>{
      this.displayTableData = res.items
      this.pager.total = res.totalCount
    })
    }else if(this.data.warehouseId){
      this.StorehouseService.getWarehouseById(this.data.warehouseId).subscribe((res:any)=>{
        this.displayTreeData = [res]
      })
      this.StorehouseService.getPositions(this.data.warehouseId,null,this.searchName,this.pager.pageIndex,this.pager.pageSize).subscribe((res:any)=>{
        this.displayTableData = res.items
        this.pager.total = res.totalCount
      })
    }else{
      this.StorehouseService.getWarehouses().subscribe((res:any)=>{
        this.treeData = res.items
        this.displayTreeData = [{ warehouseName: '全部',warehouseId:null}, ...this.treeData];
      })
      this.StorehouseService.getPositions(null,null,this.searchName,this.pager.pageIndex,this.pager.pageSize).subscribe((res:any)=>{
        this.displayTableData = res.items
        this.pager.total = res.totalCount
      })
    }
  }
  radio(event: any,rowItem:any){
    if(event.target.checked){
      this.isSelected = true;
      this.selectData = rowItem
    }
  }
  pageIndexChange($event: any) {
    this.pager.pageIndex=$event;
    this.StorehouseService.getPositions(this.data.warehouseId,null,this.searchName,this.pager.pageIndex,this.pager.pageSize).subscribe((res:any)=>{
      this.displayTableData = res.items
      this.pager.total = res.totalCount
    })
  }//切换页码
  pageSizeChange($event: any){
    this.StorehouseService.getPositions(this.data.warehouseId,null,this.searchName,this.pager.pageIndex,this.pager.pageSize).subscribe((res:any)=>{
      this.displayTableData = res.items
      this.pager.total = res.totalCount
    })
  }//改变每页显示数量
  search(){
    this.StorehouseService.getPositions(this.data.warehouseId,null,this.searchName,this.pager.pageIndex,this.pager.pageSize).subscribe((res:any)=>{
      this.displayTableData = res.items
      this.pager.total = res.totalCount
    })
  }
  search2(){
    this.pager.pageIndex = 1
    this.StorehouseService.getPositions(this.data.warehouseId,null,this.searchName,this.pager.pageIndex,this.pager.pageSize).subscribe((res:any)=>{
      this.displayTableData = res.items
      this.pager.total = res.totalCount
    })
  }
  reset(){
    this.searchName = ''
    this.StorehouseService.getPositions(this.data.warehouseId,null,this.searchName,this.pager.pageIndex,this.pager.pageSize).subscribe((res:any)=>{
      this.displayTableData = res.items
      this.pager.total = res.totalCount
    })
  }
  searchMaterialCategory($event:any){
    this.searchMaterialCategoryValue = $event.target.value;
    if (this.searchMaterialCategoryValue === '') {
      // 如果用户输入的值为空，显示原先的数据
      this.displayTreeData = this.displayTreeData = [{ warehouseName: '全部',warehouseId:null}, ...this.treeData];
    } else {
      // 否则，显示过滤后的数据
      this.filteredTreeNodes = [...this.treeData].filter((node:any) => node.warehouseName.includes(this.searchMaterialCategoryValue));
      this.displayTreeData = this.filteredTreeNodes;
    }
  }//搜索仓库类目
  afterTreeInit($event: any){
    if($event["null"]){
      $event["null"].data.isActive=true
    }
  }//树形菜单初始化
  nodeSelected($event:any){
    this.option=$event.data.originItem
    this.warehouseId = $event.data.originItem.warehouseId;
    this.StorehouseService.getPositions(this.warehouseId,null,this.searchName,this.pager.pageIndex,this.pager.pageSize).subscribe((res:any)=>{
      this.displayTableData = res.items
      this.pager.total = res.totalCount
    })
  }//树形菜单点击事件
}
